package com.zy.je.common.core.exception;

import com.zy.je.common.core.model.ICodeMessage;
import com.zy.je.common.core.web.rest.ResultStatus;

/**
 * @author zy
 */
public class BizException extends RuntimeException {
    private ICodeMessage errorCode;
    private String description;

    public BizException(ICodeMessage errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
        this.description = errorCode.getMessage();
    }

    public BizException(ICodeMessage errorCode, String description) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
        this.description = description;
    }


    public BizException(ICodeMessage errorCode, Throwable cause) {
        super(cause.getMessage(), cause);
        this.errorCode = errorCode;
        String description = null;
        if (cause instanceof BizException) {
            BizException exp = (BizException) cause;
            description = exp.getDescription();
        } else {
            description = cause.getMessage();
        }
        if (null == description || description.isEmpty()) {
            description = errorCode.getMessage();
        }
        this.description = description;
    }

    public BizException() {
    }

    @Deprecated
    public BizException(String message, Throwable cause) {
        super(message, cause);
        this.errorCode = ResultStatus.BUSINESS_ERROR;
        this.description = message;
    }

    public BizException(String message) {
        super(message);
        this.errorCode = ResultStatus.BUSINESS_ERROR;
        this.description = message;
    }

    public BizException(Throwable cause) {
        super(cause);
        if (cause instanceof BizException) {
            BizException exp = (BizException) cause;
            this.errorCode = exp.errorCode;
            this.description = exp.getDescription();
        } else {
            this.errorCode = ResultStatus.BUSINESS_ERROR;
            this.description = cause.getMessage();
        }
    }

    public void setErrorCode(ICodeMessage errorCode) {
        this.errorCode = errorCode;
    }

    public ICodeMessage getErrorCode() {
        return errorCode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
